﻿@page "/"
@using Markdig
@using Markdig.Syntax
@using Markdig.Syntax.Inlines
@using Newtonsoft.Json
<PageTitle>主页</PageTitle>


<GridRow Class="block" Align="center" Type="flex">
    <GridCol Xs="24" Sm="24" Md="24" Lg="9" Xl="9" Xxl="9">
        <Flex Justify="center" Align="center" Style="padding: 50px;">
            <img alt="" Src="favicon.png" style="width: 100%"/>
        </Flex>
    </GridCol>
    <GridCol Xs="24" Sm="24" Md="24" Lg="15" Xl="15" Xxl="15">
        <GridRow Class="block">
            @foreach (var item in Cards)
            {
                <GridCol Xs="24" Sm="24" Md="12" Lg="12" Xl="8" Xxl="8">
                    @if (string.IsNullOrEmpty(item.Url))
                    {
                        <Card Hoverable Class="card">
                            <div class="card-d">@item.ImageUrl</div>
                            <h3>@item.Title</h3>
                            <Paragraph Type="secondary">@item.Content</Paragraph>
                        </Card>
                    }
                    else
                    {
                        <a href="@item.Url" target="_blank">
                            <Card Hoverable Class="card">
                                <div class="card-d">@item.ImageUrl</div>
                                <h3>@item.Title</h3>
                                <Paragraph Type="secondary">@item.Content</Paragraph>
                            </Card>
                        </a>
                    }
                </GridCol>
            }
        </GridRow>
    </GridCol>
</GridRow>

<Flex Justify="center">
    <div style="width: 80%">
        <MarkdownRenderer html="@Content"/>
    </div>
</Flex>



@code
{
    private record CardModel(string ImageUrl, string Title, string Content, string Url = "");

    private static CardModel[] Cards { get; set; } = [];
    private string Content { get; set; } = "";

    protected override async Task OnParametersSetAsync()
    {
        Cards = JsonConvert.DeserializeObject<CardModel[]>(await File.ReadAllTextAsync("wwwroot/data/home.json")) ?? [];
        var info = new FileInfo("wwwroot/Docs/home.md");
        if (!info.Exists) return;

        var pipeline = new MarkdownPipelineBuilder()
            .UseAdvancedExtensions()
            .UseBootstrap()
            .UseEmojiAndSmiley()
            .UseSmartyPants()
            .UseSoftlineBreakAsHardlineBreak()
            .Build();
        var content = await File.ReadAllTextAsync(info.FullName);

        var document = Markdown.Parse(content, pipeline);
        foreach (var node in document)
        {
            if (node is not ParagraphBlock { Inline: not null } paragraphBlock) continue;
            foreach (var inline in paragraphBlock.Inline)
            {
                if (inline is not LinkInline { IsImage: true } linkInline) continue;
                if (!linkInline.Url!.StartsWith("http"))
                    linkInline.Url = $"Docs/Image/{linkInline.Url}";
            }
        }

        Content = document.ToHtml();
    }
}

<style>
    .block {
        padding: 40px;
        min-height: 80vh;
    }

    .card {
        margin: 10px;
        height: 200px;
        max-width: 100%;
        border-radius: 10px;
        background: #f6f6f7;
    }

    @@media screen and (max-width: 1250px) {
        .block {
            padding: 40px;
            min-height: 0;
        }
    }

    .card-d {
        font-size: 22px;
    }
</style>